<?php


namespace app\framework;


use think\Controller;

/**
 * 基础控制器
 */
class BaseController extends Controller
{
    const SC_OK = 200;
    const SC_FAIL = 0;
    const SC_FORBIDDEN = 403;
    const SC_INTERNAL_SERVER_ERROR = 500;
    const MSG_TOKEN_ERROR = '无效身份令牌';
    const MSG_OK = '操作成功！';
    const MSG_FAIL = '操作失败！';
    const MSG_FAIL_REQUEST = '操作失败，请求服务出现错误！';
    const MSG_JS_CODE_NOT_BLANK = '登录凭证不允许为空';
    const MSG_FAIL_CONNECT_WX_SERVER = '请求微信服务失败';

    /**
     * 输出基础JSON
     * @param null $data
     * @param null $message
     * @param null $status
     * @return false|string
     */
    public static function writeJSON($data = null,$message = null,$status = null){
        $json = array(
            'data'=> $data,
            'message' => $message,
            'status' => $status,
            'time'=> date('Y-m-d H:i:s'),
        );
        header("Content-Type:text/html;charset=UTF-8");
        echo json_encode($json);
    }

    /**
     * 根据请求中携带的token身份令牌，进行解密获得open_id
     * 注意：这里不进行数据库身份比对，因为在 thinkphp/library/think/App.php中已经进行身份合法性校验
     * @return string
     */
    protected function getUserOpenId(){
        $token = $_SERVER['HTTP_TOKEN'];
        $openId = Utils::authCode($token,true);
        return $openId;
    }
}